package com.suredy.core.service;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

@Service
@Lazy(false)
public class ApplicationContextHelper
  implements ApplicationContextAware
{
  private static ApplicationContext applicationContext;

  @DependsOn
  public void setApplicationContext(ApplicationContext context)
    throws BeansException
  {
    setApplicationCtx(context);
  }

  public static ApplicationContext getApplicationContext() {
    return applicationContext;
  }

  private static void setApplicationCtx(ApplicationContext context) {
    applicationContext = context;
  }

  public static final <T> T getBeanByType(Class<T> clazz)
  {
    return applicationContext.getBean(clazz);
  }
}